package com.example.listviewcontacts;

import java.sql.SQLException;
import java.util.HashMap;

import com.example.listviewcontacts.ContactInfoDBHelper;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.preference.SwitchPreference;
import android.text.TextUtils;

public class ContactInfoContentProvider extends ContentProvider {

	private SQLiteDatabase db;

	static final String AUTHORITY = "com.example.listviewcontacts.contentprovider";
	static final String BASE_PATH = "myprovider";
	static final String URL = "content://" + AUTHORITY + "/" + BASE_PATH;
	static final Uri CONTENT_URI = Uri.parse(URL);

	static final String _ID = "_id";
	static final String SIP = "sip";
	static final String NAME = "name";
	static final String SURNAME = "surname";
	static final String KEY = "key";
	static final String EXPIRE = "expire";

	private static HashMap<String, String> CONTACTS_PROJECTION_MAP;

	static final int CONTACTS = 1;
	static final int CONTACTS_ID = 2;

	static final UriMatcher uriMatcher;
	static {
		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		uriMatcher.addURI(AUTHORITY, BASE_PATH, CONTACTS);
		uriMatcher.addURI(AUTHORITY, BASE_PATH + "/#", CONTACTS_ID);
	}

	private ContactInfoDBHelper helper;

	@Override
	public boolean onCreate() {

		Context context = getContext();
		helper = new ContactInfoDBHelper(context);

		db = helper.getReadableDatabase();
		return (db == null) ? false : true;

	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {

		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
		queryBuilder.setTables(ContactInfoDBHelper.TABLE_NAME);

		switch (uriMatcher.match(uri)) {
		case CONTACTS:
			queryBuilder.setProjectionMap(CONTACTS_PROJECTION_MAP);
			break;
		case CONTACTS_ID:
			queryBuilder.appendWhere(_ID + "=" + uri.getPathSegments().get(1));
			break;

		default:
			throw new IllegalArgumentException("Unknown uri " + uri);
		}

		Cursor c = queryBuilder.query(db, projection, selection, selectionArgs,
				null, null, sortOrder);
		c.setNotificationUri(getContext().getContentResolver(), uri);
		return c;

	}

	@Override
	public String getType(Uri uri) {

		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {

		long rowID = db.insert(ContactInfoDBHelper.TABLE_NAME, "", values);

		if (rowID > 0) {
			Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
			getContext().getContentResolver().notifyChange(_uri, null);
			return _uri;
		}
		return Uri.parse(BASE_PATH + "/" + rowID);

	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int count = 0;
		switch (uriMatcher.match(uri)) {
		case CONTACTS:
			count = db.delete(ContactInfoDBHelper.TABLE_NAME, selection,
					selectionArgs);
			break;
		case CONTACTS_ID:
			String id = uri.getPathSegments().get(1);
			count = db.delete(ContactInfoDBHelper.TABLE_NAME, _ID
					+ " = "
					+ id
					+ (!TextUtils.isEmpty(selection) ? " AND (" + selection
							+ ')' : ""), selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown Uri " + uri);

		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {

		int count = 0;
		switch (uriMatcher.match(uri)) {
		case CONTACTS:
			count = db.update(ContactInfoDBHelper.TABLE_NAME, values,
					selection, selectionArgs);
			break;
		case CONTACTS_ID:
			count = db.update(
					ContactInfoDBHelper.TABLE_NAME,
					values,
					_ID
							+ " = "
							+ uri.getPathSegments().get(1)
							+ (!TextUtils.isEmpty(selection) ? " AND ("
									+ selection + ')' : ""), selectionArgs);

			break;

		default:
			throw new IllegalArgumentException("Unknown uri " + uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return count;

	}

}
